public class Text {

    /**
     * 复写零
     * @param arr
     */
    public void duplicateZeros1(int[] arr) {
        int dest = -1;
        int cur = 0;
        while (dest < arr.length - 1 ){
            if(arr[cur] == 0){
                dest+=2;
            }
            dest++;
            cur++;
        }
        while (cur >= 0){
            if(arr[cur] == 0){
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            }else {
                arr[dest--] = arr[cur--];
            }
        }
    }

    public void duplicateZeros2(int[] arr) {
        int dest = -1;
        int cur = 0;
        while (cur < arr.length ){
            if(arr[cur] == 0) dest+=2;
            else dest++;
            if(dest >= arr.length - 1) break;
            cur++;
        }
        if(dest == arr.length){
            arr[dest-1] = 0;
            dest-=2;
            cur--;
        }
        while (cur >= 0){
            if(arr[cur] == 0){
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            }else {
                arr[dest--] = arr[cur--];
            }
        }
    }

    public void duplicateZeros(int[] arr) {
    int[] nums = new int[arr.length];

    int dest = 0;
    for (int cur = 0; dest < arr.length; cur++) {
        if(arr[cur] != 0){
            nums[dest++] = arr[cur];
        }else {
            nums[dest++] = arr[cur];
            nums[dest++] = 0;
        }
    }

        arr = nums;
    }

    /**
     *  移动零
     *  时间复杂度 O(N)
     * @param nums
     */
    public void moveZeroes(int[] nums) {
        int dest = -1;
        int cur = 0;
        while (cur < nums.length){
            if(nums[cur] != 0){
                swap(nums,dest+1,cur);
                dest++;
            }
            cur++;
        }
    }

    public static void swap(int[] array,int x,int y){
        int tmp = array[x];
        array[x] = array[y];
        array[y] = tmp;
    }

    public static void main(String[] args) {

    }
}
